package cn.jzk.mySpringSecurity_noSeparation.Common.Utils;

import cn.jzk.mySpringSecurity_noSeparation.Entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author jiangzk
 * 2025/1/23  上午9:01
 */
public class SecurityUtils {

    /**
     * 获取已经认证成功的 Authentication 对象
     */
    public static Authentication getAuthentication() {

        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取当前认证用户的信息
     **/
    public static LoginUser getLoginUser() {

        return (LoginUser) getAuthentication().getPrincipal();
    }

    /**
     *  获取当前认证用户的用户 ID
     */
    public static Integer getUserId() {

        return getLoginUser().getUser().getId();
    }
}
